package com.coderman.infosys.auth.infrast.mq.consumer;


import com.alibaba.fastjson.JSON;
import com.coderman.infosys.auth.domain.bo.msgbody.UserStatusChangeMsgBody;
import com.coderman.infosys.auth.domain.gataway.UserRoleQueryGataway;
import com.coderman.infosys.auth.domain.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @Description:监听用户账号状态变化消息
* @Author:shenshuai
* @CreateTime:2022-01-02 15:27:38
* @version v1.0
*/
@Service
public class UserMqConsumer implements MqConsumeHandler{

   @Autowired
   private UserRoleRepository userRoleRepository;

   @Autowired
   private UserRoleQueryGataway userRoleQueryGataway;

   @Override
   public boolean consume(String jsonMsgBody) {

      //1.将消息内容转成MsgBody
      UserStatusChangeMsgBody userStatusChangeMsgBody = JSON.parseObject(jsonMsgBody,UserStatusChangeMsgBody.class);
      //
      //场景：用户离职---将用户与角色解除绑定，如果用户账号与组织存在关系，且在权限系统里保留这里一并解除绑定，将用户从用户组中移除
      //这里我们演示第一种处理方式，直接在UserMqConsumer监听器里实现相关逻辑
      userRoleRepository.removeUser(userStatusChangeMsgBody.getUserId());

      //第二种处理方式：通过查询网关查询出存在关联关系的角色-用户和用户组，直接在这里操作



      return true;
   }
}